//
//  GHUtils.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "TMUtils.h"
#import "TMGFlowViewController.h"
#import "TMGNewFlowGoldDetailController.h"
#import "TMGNewLingQianDetailController.h"
#import "TMGNewMonthDetailController.h"
#import "TMGFlowGoldBuyViewController.h"
#import "TMGDiscountModel.h"
#define goldPriceKey @"goldPriceKey"
@implementation TMUtils
+ (void)netWorkMonitorinCanUseWithHandle:(void (^)())handle{
    
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
            if (handle) {
                handle();
            }
        }
    }];
    [manager startMonitoring];
}
+ (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    sectionHeader.backgroundColor = [UIColor whiteColor];
    STButton * line = [[STButton alloc] initWithFrame:CGRectMake(15, 0, 4, 22)
                                                title:nil
                                           titleColor:nil
                                            titleFont:14
                                         cornerRadius:0
                                      backgroundColor:TM_orangeColor
                                      backgroundImage:nil
                                                image:nil];
    line.centerY = 22;
    [sectionHeader addSubview:line];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(line.right + 10, 0, 200, 44)
                                                     text:title
                                                textColor:TM_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    
    [sectionHeader addSubview:titleLable];
    return sectionHeader;
}
+ (UIView*)headerViewWithTitle:(NSString*)title color:(UIColor*)coclor{
    UIView * sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    sectionHeader.backgroundColor = [UIColor whiteColor];
    STButton * line = [[STButton alloc] initWithFrame:CGRectMake(15, 0, 4, 22)
                                                title:nil
                                           titleColor:nil
                                            titleFont:14
                                         cornerRadius:0
                                      backgroundColor:coclor
                                      backgroundImage:nil
                                                image:nil];
    line.centerY = 22;
    [sectionHeader addSubview:line];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(line.right + 10, 0, 200, 44)
                                                     text:title
                                                textColor:TM_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    
    [sectionHeader addSubview:titleLable];
    return sectionHeader;
}
+ (UINavigationBar *)navigationBar{
    UINavigationController* nav = (id)[UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers.firstObject;
    return nav.navigationBar;
}
+  (UITabBar *)tabbar{
    UITabBarController* tab = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
    return tab.tabBar;
}
+ (CGFloat)navgationBarBootom{
    if (KIsiPhoneX) {
        return 88;
    }
    return 64;
}
+ (CGFloat)tabBarTop{
    if (KIsiPhoneX) {
        return (UIScreenHeight - 83);
    }
    return (UIScreenHeight - 49);
}
+ (NSString *)lastGoldMoney{
    NSString * gold = [[NSUserDefaults standardUserDefaults] valueForKey:goldPriceKey];
    return gold;
}
+ (void)fetchCurrentGoldPrice:(void (^)(TMCurrentColdModel *))handle{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",@"http://apis.haoservice.com/lifeservice/gold/shgold?key=",goldKey];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 15;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    
    [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray * array = [responseObject valueForKey:@"result"];
        if ([array isKindOfClass:[NSArray class]]) {
            for (NSDictionary * dataDic in array) {
                TMCurrentColdModel * model = [TMCurrentColdModel mj_objectWithKeyValues:dataDic];
                if ([model.variety isEqualToString:goldVariety]) {
                    [[NSUserDefaults standardUserDefaults] setObject:model.latestpri forKey:goldPriceKey];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    if (handle) {
                        handle(model);
                    }
                }
            }
            NSLog(@"%@",array.mj_JSONString);
        }

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
+ (void)fetchGreenHandGoldPrice:(void (^)(TMGProductModel *))handle{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/goods/getGoods"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [dic setObject:@"1" forKey:@"cate"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array =  responseObject[@"data"];
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 NSDictionary * dataDic = array.firstObject;
                                                 TMGProductModel * model = [TMGProductModel mj_objectWithKeyValues:dataDic];
                                                 if (handle) {
                                                     handle(model);
                                                 }
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 if (handle) {
                                                     handle(nil);
                                                 }
                                             }];
}
+ (void)fetchPersonnalInfo:(void (^)(TMGUserModel *))handle{
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/getPersonal"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 TMGUserModel * user = [TMGUserModel mj_objectWithKeyValues:responseObject[@"data"]];
                                                 [[STUserManger defult] updateUserModel:user];
                                                 if (handle) {
                                                     handle(user);
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 if (handle) {
                                                     handle(nil);
                                                 }
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
    
    
}
+ (NSString *)releaseUrlWithUrl:(NSString *)url{
    if ([url containsString:@"http://pjjzone.top"]) {
        url = [url stringByReplacingOccurrencesOfString:@"http://pjjzone.top" withString:service_header];
    }
    return url;
}

+ (void)gotoCustomServiceViewController{
    UITabBarController * tabBar = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
    if ([tabBar isKindOfClass:[UITabBarController class]]) {
        UINavigationController * nav = tabBar.childViewControllers[tabBar.selectedIndex];
        //push
        KLMessageChatViewController * vc = [[KLMessageChatViewController alloc] initWithConversationChatter:@"service_one" conversationType:EMConversationTypeChat];
        [nav pushViewController:vc animated:YES];
    }
}
#pragma mark ----流动金
+ (void)gotoBuyFlowGold{
    
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/goods/getGoods"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [dic setObject:@"2" forKey:@"cate"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {

                                                 [SVProgressHUD dismiss];

                                                 NSArray * array =  responseObject[@"data"];
                                                 NSDictionary * modelDic = array.firstObject;
                                                 TMGProductModel * model = [TMGProductModel mj_objectWithKeyValues:modelDic];
                                                 UITabBarController * tabBar = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
                                                 if ([tabBar isKindOfClass:[UITabBarController class]]) {
                                                     UINavigationController * nav = tabBar.childViewControllers[tabBar.selectedIndex];
                                                     //push
                                                     TMGFlowViewController * vc = [TMGFlowViewController new];
                                                     vc.model = model;
                                                     [nav pushViewController:vc animated:YES];
                                                 }

                                                 
                                             } failure:^(NSString *stateCode, STError *error) {

                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
    

}
+ (void)gotoNewBuyFlowGold{
    
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/goods/getGoods"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [dic setObject:@"2" forKey:@"cate"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 
                                                 NSArray * array =  responseObject[@"data"];
                                                 NSDictionary * modelDic = array.firstObject;
                                                 TMGProductModel * model = [TMGProductModel mj_objectWithKeyValues:modelDic];
                                                 UITabBarController * tabBar = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
                                                 if ([tabBar isKindOfClass:[UITabBarController class]]) {
                                                     UINavigationController * nav = tabBar.childViewControllers[tabBar.selectedIndex];
                                                     //push
                                                     TMGNewFlowGoldDetailController * vc = [[TMGNewFlowGoldDetailController alloc] initWithStyle:UITableViewStyleGrouped];
                                                     vc.model = model;
                                                     [nav pushViewController:vc animated:YES];
                                                 }
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
    
    
}
+ (void)gotoNewBuyFlowOrderGold{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/goods/getGoodsDetails"];
    [dic setObject:@"2" forKey:@"gid"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 TMGProductDetailModel * detailModel = [TMGProductDetailModel mj_objectWithKeyValues:dataDic];
                                                 detailModel.cate = @"2";
                                                 UITabBarController * tabBar = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
                                                 if ([tabBar isKindOfClass:[UITabBarController class]]) {
                                                     UINavigationController * nav = tabBar.childViewControllers[tabBar.selectedIndex];
                                                     //push
                                                     TMGFlowGoldBuyViewController * vc = [TMGFlowGoldBuyViewController new];
                                                     vc.model = detailModel;
                                                     [nav pushViewController:vc animated:YES];
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {

                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
//去购买零钱计划
+ (void)gotoBuyLingqianGold{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/goods/getGoods"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [dic setObject:@"4" forKey:@"cate"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 
                                                 NSArray * array =  responseObject[@"data"];
                                                 NSDictionary * modelDic = array.firstObject;
                                                 TMGProductModel * model = [TMGProductModel mj_objectWithKeyValues:modelDic];
                                                 UITabBarController * tabBar = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
                                                 if ([tabBar isKindOfClass:[UITabBarController class]]) {
                                                     UINavigationController * nav = tabBar.childViewControllers[tabBar.selectedIndex];
                                                     //push
                                                     TMGNewLingQianDetailController * vc = [TMGNewLingQianDetailController new];
                                                     vc.model = model;
                                                     [nav pushViewController:vc animated:YES];
                                                 }
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
//去购买月月生息
+ (void)gotoBuyYueYueGold{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/goods/getGoods"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [dic setObject:@"6" forKey:@"cate"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 
                                                 NSArray * array =  responseObject[@"data"];
                                                 NSDictionary * modelDic = array.firstObject;
                                                 TMGProductModel * model = [TMGProductModel mj_objectWithKeyValues:modelDic];
                                                 UITabBarController * tabBar = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
                                                 if ([tabBar isKindOfClass:[UITabBarController class]]) {
                                                     UINavigationController * nav = tabBar.childViewControllers[tabBar.selectedIndex];
                                                     //push
                                                     TMGNewMonthDetailController * vc = [TMGNewMonthDetailController new];
                                                     vc.model = model;
                                                     [nav pushViewController:vc animated:YES];
                                                 }
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
+ (void)fecthOnSaleRequestWithGoodTitle:(NSString *)goodTitle money:(NSString *)money handle:(void (^)(NSArray *))handle{
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/getUsefulCouple"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    NSString * g_type = @"";
    if ([goodTitle isEqualToString:@"新手特权金"]) {
        g_type = @"1";
    }
    if ([goodTitle isEqualToString:@"口袋金"]) {
        g_type = @"";
    }
    if ([goodTitle isEqualToString:@"压箱金7天"]) {
        g_type = @"3";
    }
    if ([goodTitle isEqualToString:@"压箱金30天"]) {
        g_type = @"4";
    }
    if ([goodTitle isEqualToString:@"压箱金90天"]) {
        g_type = @"5";
    }
    if ([goodTitle isEqualToString:@"压箱金180天"]) {
        g_type = @"6";
    }
    if ([goodTitle isEqualToString:@"压箱金365天"]) {
        g_type = @"7";
    }
    if ([goodTitle isEqualToString:@"安心计划30天"]) {
        g_type = @"9";
    }
    if ([goodTitle isEqualToString:@"安心计划60天"]) {
        g_type = @"10";
    }
    if ([goodTitle isEqualToString:@"安心计划180天"]) {
        g_type = @"11";
    }
    if ([goodTitle isEqualToString:@"安心计划365天"]) {
        g_type = @"12";
    }
    if (!g_type.length) {
        if (handle) {
            handle(nil);
        }
        return;
    }
    [dic setObject:g_type forKey:@"g_type"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 NSArray * array = responseObject[@"data"];
                                                 NSMutableArray * dataSouce = [NSMutableArray new];
                                                 
                                                 for (NSDictionary *modelDic in array) {
                                                     TMGDiscountModel * model = [TMGDiscountModel mj_objectWithKeyValues:modelDic];
                                                     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                                                     [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                                                     NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
                                                     [formatter setTimeZone:GTMzone];
                                                     NSDate * date = [formatter dateFromString:model.end];
                                                     NSInteger num = [date timeIntervalSince1970];
                                                     NSInteger now = [[NSDate st_localCurrentDate] timeIntervalSince1970];
                                                     //判断金额 和是否失效
                                                         if (model.more.floatValue < money.floatValue&&num >= now) {
                                                             [dataSouce addObject:model];
                                                         }
                                                     }
                                                 if (handle) {
                                                     handle(dataSouce);
                                                 }
                                                 
     
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 if (handle) {
                                                     handle(nil);
                                                 }
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end

